<?php

namespace app\membercall\controller;
use Think\Db;
use think\Validate;

/**
 * 商品控制器
 * @package app\api\controller
 */
class Engineer extends Home
{
    public $noNeedLogin = [];

    /**
     * 获取技师列表
     */
    public function index(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            if(empty($getData['store_id'])){
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_users')
                ->field('id,work_number,work_status')
                ->where(['role'=>'3','store_id'=>$getData['store_id']])
                ->paginate($getData['limit']);
            $this->result('', $data, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }

    /**
     * 搜索技师
     */
    public function search(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['store_id']) || empty($getData['engineer_id'])){
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_users')
                ->field('id,work_number,work_status')
                ->where(['id'=>$getData['engineer_id'],'store_id'=>$getData['store_id']])
                ->find();
            $this->result('', $data, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }


    /**
     * 技师排队
     */
    public function lineup(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'store_id|门店' => 'require|number',
                'engineer_id|技师' => 'require|number',
                'hand_card_id|手牌' => 'require|number',
                'room_id|房间' => 'require|number',
                'project_id|项目' => 'require|number',
            ];
            // 定义验证提示
            $message = [
                'store_id.require' => '门店不能为空',
                'store_id.number' => '门店格式不正确',
                'engineer_id.require' => '技师不能为空',
                'engineer_id.number' => '技师格式不正确',
                'hand_card_id.require' => '手牌不能为空',
                'hand_card_id.number' => '手牌格式不正确',
                'room_id.require' => '房间不能为空',
                'room_id.number' => '房间格式不正确',
                'project_id.require' => '项目不能为空',
                'project_id.number' => '项目格式不正确',

            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            //插入数据
            $data['create_time'] = time();
            $find = Db::name('qt_lineup')
                ->where(['engineer_id'=>$data['engineer_id']])
                ->whereTime('create_time','today')
                ->order('id desc')
                ->find();
            //如果前面有排队的时间在原有基础上加十分钟否则就是十分钟
            if(empty($find)){
                $estimate_time = '10';
            }else{
                $time = Db::name('qt_project')->where('id',$data['project_id'])->value('time');
                $estimate_time = $find['estimate_time'] + 10 + $time;
            }
            $data['estimate_time'] = $estimate_time;
            $save = Db::name('qt_lineup')->insert($data);
            if($save){
                $this->result('排队成功', '', 200);
            }
            $this->result('排队失败', '', 204);

        }
        $this->result('无效的请求方式', '', 204);
    }



}
